<?php

require_once('db_connection.php');
	
function login($username, $password)
{
	if(!$username || !$password) return false;
	
	$query = "SELECT * FROM `sailors` WHERE username ='".mysql_escape_string($username)."' AND password ='".md5($password)."'";
	$result = mysql_query($query);	
	if(!$result) return false;
	
	if( mysql_num_rows($result) > 0 )
	{
		$row = mysql_fetch_array($result);
		return $row;
	}
	else
	{
		return false;
	}
}

function add_new_user($username, $password, $name, $age=null)
{
	if(!$username || !$password || !$name) return false;
	
	$query = "SELECT * FROM `sailors` WHERE username ='".mysql_escape_string($username)."'";
	$result = mysql_query($query);	
	if(!$result) return false;
	
	if( mysql_num_rows($result) > 0 )
	{
		$_SESSION['response_msg'] = "Username already exists!";
		return false;
	}
	else
	{
		$query = "INSERT INTO `sailors`(username, password,sname, age) VALUES ('".mysql_real_escape_string($username)."','".md5($password)."','".mysql_real_escape_string($name)."','".mysql_real_escape_string($age)."')";
		$result = mysql_query($query);	

		if($result) return true;
		else return false;
	}
}

function add_new_boat($boatname, $boatcolor)
{
	if(!$boatname || !$boatcolor) return false;
	
	$query = "INSERT INTO `boats`(bname, color) VALUES ('".mysql_real_escape_string($boatname)."','".mysql_real_escape_string($boatcolor)."')";
	$result = mysql_query($query);	
	if($result) return mysql_insert_id();
	else return false;

}	

function get_boat_list()
{
	$query = "SELECT * FROM `boats`";
	$result = mysql_query($query);	
	
	if($result)
	{
		$data = array();
		while ( $row = mysql_fetch_array($result) )	array_push($data , $row);
			
		return $data;
	}
	else
	{
		return false;
	}
}

function update_boat($bid, $boatname, $boatcolor)
{
	if(!$bid || !$boatname || !$boatcolor) return false;

	$query = "UPDATE `boats` SET bname ='".mysql_real_escape_string($boatname)."', color = '".mysql_real_escape_string($boatcolor)."' WHERE bid = '".mysql_real_escape_string($bid)."'";
	$result = mysql_query($query);	
	
	if($result)		return true;
	else			return false;
}

function delete_boat($bid)
{
	if(!$bid ) return false;

	$query = "DELETE FROM `boats` WHERE bid = '".mysql_real_escape_string($bid)."'";
	$result = mysql_query($query);	
	
	if($result)		return true;
	else			return false;
}

function search_boat($boatname=null, $boatcolor=null)
{
	if(!$boatname && !$boatcolor) return false;

	$query = "SELECT * FROM `boats` WHERE 1";
	if($boatname) $query .= " AND bname LIKE '%".mysql_real_escape_string($boatname)."%' ";
	if($boatcolor) $query .= " AND color LIKE '%".mysql_real_escape_string($boatcolor)."%' ";
	
	$result = mysql_query($query);	
	
	if($result)
	{
		$data = array();
		while ( $row = mysql_fetch_array($result) )	array_push($data , $row);
			
		return $data;
	}
	else
	{
		return false;
	}
}

function add_new_reservation($sailor_id, $boat_id, $day)
{
	if(!$sailor_id || !$boat_id || !$day) return false;
	
	$query = "SELECT * FROM `reserves` WHERE sid ='".mysql_real_escape_string($sailor_id)."' AND bid = '".mysql_real_escape_string($boat_id)."' AND day = '".mysql_real_escape_string($day)."'";
	$result = mysql_query($query);	
	if(!$result) return false;
	
	if( mysql_num_rows($result) > 0 )
	{
		
		$_SESSION['response_msg'] = "Boat not available on the requested day";
		return false;
	}
	else
	{
		$query = "INSERT INTO `reserves`(sid, bid, day) VALUES ('".mysql_real_escape_string($sailor_id)."','".mysql_real_escape_string($boat_id)."','".mysql_real_escape_string($day)."')";
		$result = mysql_query($query);	
		if($result) return true;
		else return false;
	}
}

function get_reservation_list($sailor_id)
{
	$query = "SELECT b.bid as boat_id, b.bname as boat_name, r.day, rs.status_desc as status FROM `reserves` r, `boats` b, reserves_status rs WHERE r.sid = '".mysql_real_escape_string($sailor_id)."' AND b.bid = r.bid AND r.status_code = rs.status_code";
	$result = mysql_query($query);	
	
	if($result)
	{
		$data = array();
		while ( $row = mysql_fetch_array($result) )	array_push($data , $row);
			
		return $data;
	}
	else
	{
		return false;
	}
}

function cancel_reservation($sailor_id, $boat_id, $date)
{
	
	if(!$sailor_id || !$boat_id || !$date) return false;
	
	$query = "UPDATE `reserves` SET status_code = '0' WHERE sid ='".mysql_real_escape_string($sailor_id)."' AND bid = '".mysql_real_escape_string($boat_id)."' AND day = '".mysql_real_escape_string($date)."'";
	$result = mysql_query($query);

	if($result) return true;
	else return false;
	
}

?>